package com.example.unlock

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import com.example.unlock.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() , IUnlockView{

    private lateinit var presenter: UnlockPresenter
    private lateinit var binding: ActivityMainBinding

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //创建绑定类的对象
        binding = ActivityMainBinding.inflate(layoutInflater)
        //将绑定类里面对应的view设置给activity
        setContentView(binding.root)

        val modelsArray = arrayListOf<ImageViewModel>()

        //将9个点的视图保存到数组中
        val dotArray = arrayOf(
            binding.dot1,
            binding.dot2,
            binding.dot3,
            binding.dot4,
            binding.dot5,
            binding.dot6,
            binding.dot7,
            binding.dot8,
            binding.dot9
        )
        dotArray.forEach {
            modelsArray.add(ImageViewModel(it, R.drawable.dot_normal, R.drawable.dot_selected))
        }
        //竖线
        val verticalLineArray = arrayOf(
            binding.line14,
            binding.line25,
            binding.line36,
            binding.line47,
            binding.line58,
            binding.line69
        )
        verticalLineArray.forEach {
            modelsArray.add(ImageViewModel(it, R.drawable.line_1_normal, R.drawable.line_1_error))
        }
        //横线
        val landscapeLineArray = arrayOf(
            binding.line12,
            binding.line23,
            binding.line45,
            binding.line56,
            binding.line78,
            binding.line89
        )
        landscapeLineArray.forEach {
            modelsArray.add(ImageViewModel(it, R.drawable.line_2_normal, R.drawable.line_2_error))
        }
        //左斜
        val leftSlashLineArray =
            arrayOf(binding.line24, binding.line35, binding.line57, binding.line68)
        leftSlashLineArray.forEach {
            modelsArray.add(ImageViewModel(it, R.drawable.line_4_normal, R.drawable.line_4_error))
        }
        //右斜
        val rightSlashLineArray =
            arrayOf(binding.line15, binding.line26, binding.line48, binding.line59)
        rightSlashLineArray.forEach {
            modelsArray.add(ImageViewModel(it, R.drawable.line_3_normal, R.drawable.line_3_error))
        }

        val lineTagsArray = listOf(
            12,23,45,56,78,89,
            14,25,36,47,58,69,
            15,26,48,59,24,35,57,68
        )
        //小王 创建presenter
        presenter = UnlockPresenter(
            this,
            dotArray.toList(),
            modelsArray.toList(),
            lineTagsArray
        )

        //小王 调用loadUserInfo
        presenter.loadUserInfo()

        binding.container.setOnTouchListener { v, event ->
            val x = event.x
            val y = event.y
            when(event.action){
                MotionEvent.ACTION_DOWN -> presenter.userTouch(x,y)
                MotionEvent.ACTION_MOVE -> presenter.userTouch(x,y)
                MotionEvent.ACTION_UP -> presenter.endTouch()
            }
            true
        }
    }

    override fun showAlertText(text: String) {
        binding.alertTitle.text = text
    }

    override fun switchActivity() {
        //界面切换
        val intent = Intent(this,HomeActivity::class.java)
        startActivity(intent)
    }

}



















